/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     uiEncoder.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/

#include "uiEncoder.h"
#include "levetop.h"
#include "data.h"
#include "rtc.h"
#include "keyBoard.h"
#include "function_drv.h"
#include "gesture.h"
#include "eflash.h"
/*encoder*/
uint16_t gEncoder_addr[40] = {0};
uint16_t Check_box_add = 0xFFFF;
uint8_t Check_box_flag = 0;
uint8_t gEncoder_count = 0;
uint8_t gEncoder_num = 0;
uint8_t knob_flag = 0;
uint8_t gEncoderFlag = 0;
uint8_t gEncoder_old_value = 0;
uint16_t gEncoder_old_icon = 0;
uint16_t gEncoder_old_x;
uint16_t gEncoder_old_y;

uint8_t knob_dir = 0, SW_flag = 0, SW_flag4 = 0, key_time = 0;
uint8_t Encoder_read_Flag = 0, Encoder_Freetime = 0;
uint8_t Enc0 = 0, Enc1 = 0;
uint8_t EncNow;
uint16_t Encoder_time = 0;
static uint8_t EncOld, EncX = 0;

uint8_t Double_click_interval = 0;
uint8_t Long_press = 0;
uint8_t Encoder_type = 0;
LT_ENCODER_ LT_encoder = {0};


uint8_t fanhui_flag = 0;
uint32_t fanhui_timesum = 0;
uint8_t BassFH_flag = 0;
uint32_t BassFH_time = 0;
uint8_t Danjisum1_flag,Danjisum2_flag;
uint8_t min_flag = 0;
uint32_t minA_timesum = 0;

uint8_t W_buff[WR_SIZE];

uint16_t JiYi_time=0;
uint8_t JiYi_flag=0;


void JiYi_ok(void)
{
	if(JiYi_flag)
	{
		JiYi_time++;
		if(JiYi_time==100)
		{
		//LTPrintf("10S_ji_ok\r\n");
		EFlash_Init();
		W_buff[0]=colour_flag;
		W_buff[1]=warmBlast_flag;
		W_buff[2]=warmlight_flag;
		W_buff[3]=coldBlast_flag;
		W_buff[4]=coldlight_flag;
		W_buff[5]=warmBlast_Mode;
		W_buff[6]=warmlight_Mode;
		W_buff[7]=coldBlast_Mode;
		W_buff[8]=coldlight_Mode;
		EFLASH_Write(0x6007F000, &W_buff[0], WR_SIZE);
		JiYi_flag=0;
		}
		//LTPrintf("JiYi_time = %d \r\n",JiYi_time);
	}
}

void send_data_ALL(uint8_t value3,uint8_t value4,uint8_t value5,uint8_t value6,uint8_t value7)
{
	uint8_t rebuf[50];
    rebuf[3] = value3;
    rebuf[4] = value4;
    rebuf[5] = value5;
	rebuf[6] = value6;
	rebuf[7] = value7;
	LT_SendData_CRC_Frame(rebuf, 5);
}


void get_encoder(uint8_t type, uint8_t *buff, uint16_t addr)
{
	uint16_t i;
	uint16_t len, len1;

	len = buff[4] + (buff[5] << 8);
	len1 = 8;
	Check_box_add = buff[6] + (buff[7] << 8);

//	Var_AddrRemapping(&Check_box_add);

	for (i = 0; i <= 255; i++)
	{
		gEncoder_addr[i] = len1 + addr;
		len1 = len1 + buff[len1 + 1] + 2;
		if (len1 - 6 >= len)
			break;
	}

	gEncoderFlag = 1;

	gEncoder_num = (var[Check_box_add * 2] << 8) + var[Check_box_add * 2 + 1];
	if (gEncoder_num > i)
		gEncoder_num = i;

	gEncoder_old_value = gEncoder_num;  // record
//	printf("gEncoder_old_value  %d  \r\n"  ,  gEncoder_old_value );

	gEncoder_count = i;
}
/***********************************************************************************
* Function Name : Display_encoder
* Description   : Run encoder.
* Input         : - type: encoder info sources
				  - buff: encoder info
* Output        : None
* Return        : None
************************************************************************************/
int warmBlast_flag=6;//666666
int warmlight_flag=6;
int coldBlast_flag=6;
int coldlight_flag=6;

int warmBlast_Mode=2;
int warmlight_Mode=2;
int coldBlast_Mode=2;
int coldlight_Mode=2;
uint8_t colour_flag=1;

void Display_encoder(uint8_t *buff, uint8_t type)
{
	uint8_t i;
	uint8_t len, loop;
	int16_t sum = 0, V_max, V_min;
	uint16_t p_add = 0, step;
	uint8_t rebuf[50];
	uint16_t addr;

	len = buff[1];

	if (type == 4 || type == 0x84) // multi button control
	{
		for (i = 0; i < 32; i += 4)
		{
			addr = (buff[i + 2] + (buff[i + 2 + 1] << 8));
			Var_AddrRemapping(&addr);

			if (addr != 0xFFFF && addr <= VAR_ADDR_E)
			{
				var[addr * 2] = buff[i + 2 + 3];
				var[addr * 2 + 1] = buff[i + 2 + 2];

				if (addr >= VAR_ADDR_S)
				{
					reg_operation(addr);
				}
			}
		}
		if (type == 0x84)
		{
			rebuf[3] = 0x41;
			for (i = 0; i < 8; i++)
			{
				rebuf[4 * i + 4] = buff[4 * i + 2 + 1];
				rebuf[4 * i + 5] = buff[4 * i + 2];
				rebuf[4 * i + 6] = buff[4 * i + 2 + 3];
				rebuf[4 * i + 7] = buff[4 * i + 2 + 2];
			}
			LT_SendData_CRC_Frame(rebuf, 33);
		}

		if ((buff[0] + (buff[1] << 8)) < addr_index[7])
			Display_page(buff[0] + (buff[1] << 8));
		else
			page_date_update(pic_id);
		}
	else if (buff[0] == 1 || buff[0] == 0x81) // Multivariable control
	{
		for (i = 0; i < (len - 2); i += 4)
		{
			addr = (buff[i + 4] + (buff[i + 4 + 1] << 8));
			Var_AddrRemapping(&addr);
			if (addr != 0xFFFF && addr <= VAR_ADDR_E)
			{
				var[addr * 2] = buff[i + 4 + 3];
				var[addr * 2 + 1] = buff[i + 4 + 2];
				if (addr >= VAR_ADDR_S)
				{
					reg_operation(addr);
				}
			}
		}

		if (buff[0] == 0x81)
		{
			rebuf[3] = 0x41;
			for (i = 0; i < 8; i++)
			{
				rebuf[4 * i + 4] = buff[4 * i + 4 + 1];
				rebuf[4 * i + 5] = buff[4 * i + 4];
				rebuf[4 * i + 6] = buff[4 * i + 4 + 3];
				rebuf[4 * i + 7] = buff[4 * i + 4 + 2];
			}
			LT_SendData_CRC_Frame(rebuf, 33);
		}

		if ((buff[2] + (buff[3] << 8)) < addr_index[7])
			Display_page(buff[2] + (buff[3] << 8));
		else
			page_date_update(pic_id);
	}
	else if (buff[0] == 2 || buff[0] == 0x82)
	{
		p_add = buff[2] + (buff[3] << 8);
		V_max = buff[4] + (buff[5] << 8);
		V_min = buff[6] + (buff[7] << 8);
		step = buff[8] + (buff[9] << 8);
		loop = buff[11];

		Var_AddrRemapping(&p_add);

		sum = (var[p_add * 2] << 8) + var[p_add * 2 + 1];

		if (buff[10] == 1) // 锟斤拷
		{
			if (sum + step > V_max)
			{
				if (loop == 1) // 循锟斤拷
				{
					var[p_add * 2] = V_min >> 8 & 0xFF;
					var[p_add * 2 + 1] = V_min & 0xFF;
				}
				else
				{
					var[p_add * 2] = V_max >> 8 & 0xFF;
					var[p_add * 2 + 1] = V_max & 0xFF;
				}
			}
			else
			{
				sum += step;
				var[p_add * 2] = sum >> 8 & 0xFF;
				var[p_add * 2 + 1] = sum & 0xFF;
			}
		}
		else if (buff[10] == 0) // 锟斤拷
		{
			if (sum - step < V_min)
			{
				if (loop == 2)
				{
					var[p_add * 2] = V_max >> 8 & 0xFF;
					var[p_add * 2 + 1] = V_max & 0xFF;
				}
				else
				{
					var[p_add * 2] = V_min >> 8 & 0xFF;
					var[p_add * 2 + 1] = V_min & 0xFF;
				}
			}
			else
			{
				sum -= step;
				var[p_add * 2] = sum >> 8 & 0xFF;
				var[p_add * 2 + 1] = sum & 0xFF;
			}
		}
		if (buff[0] == 0x82)
		{
			rebuf[3] = 0x41;
			rebuf[4] = buff[3];
			rebuf[5] = buff[2];
			rebuf[6] = var[p_add * 2];
			rebuf[7] = var[p_add * 2 + 1];

			Var_AddrToReg(&rebuf[4]);

			LT_SendData_CRC_Frame(rebuf, 5);
		}
		if (p_add >= VAR_ADDR_S)
			reg_operation(p_add);

		page_date_update(pic_id);
	}
	else if (buff[0] == 3 || buff[0] == 0x83)
	{
		if (knob_flag == 0)
			knob_flag = 1;
		else if (knob_flag == 1)
			knob_flag = 0;
	}
}

/***********************************************************************************
 * Function Name : encoder
 * Description   : Detect encoder rotation and key.
 * Input         : None
 * Output        : None
 * Return        : None
 ************************************************************************************/
int VOLgoto_flag=1;
uint8_t Danji_flag,Danji1_flag;
uint8_t Play_Order;
static const int varAddr[] = {
    0x0AB1*2+1,
    0x0AB2*2+1,
    0x0AB3*2+1,
    0x0AB4*2+1,
    0x0AB5*2+1
};
void encoder(void)
{
	uint16_t addr;
	uint16_t p_add, step;
	uint8_t type;
	uint8_t rebuf[10];
	int16_t V_max, V_min, sum = 0;

	if (Enc0 == 0)
		EncOld = (PINA ? 0x02 : 0x00) + (PINB ? 0x01 : 0x00); // Remember the status of the encoder at the first call
	if (EncOld == 0x00 || EncOld == 0x03)
		Enc0 = 1;

	// According to the current status of two IO, they are combined into a hexadecimal 0x00 | 0x01 | 0x02 | 0x03
	EncNow = (PINA ? 0x02 : 0x00) + (PINB ? 0x01 : 0x00);

	if ((EncOld == 0x00 && EncNow == 0x02) || (EncOld == 0x03 && EncNow == 0x01))
		EncX = EncNow; // 00-10|11-01

	// turn right,00-10-11|11-01-00
	if ((EncOld == 0x00 && EncX == 0x02 && EncNow == 0x03) || (EncOld == 0x03 && EncX == 0x01 && EncNow == 0x00))
	{
		Encoder_Freetime = 0;
		EncOld = EncNow, EncX = 0;
		Backlight_count = 0;
		//LTPrintf("110\r\n");
		if (var[VAR_BLAUTO * 2 + 1] ==1 && Backlight_flag == 0 )
		{
			Backlight_flag = 1;
			LT_BacklightSet(gDutyBuf[var[VAR_BL * 2 + 1]]);
		}
		else if (SW_flag == 0 && SW_flag4 == 0)
		{
			if(EncType == 1)
			{
				if (knob_flag == 1) // Move right at knob control variable to increase
				{
					addr = gEncoder_addr[gEncoder_num];
					p_add = config_buff[addr + 2] + (config_buff[addr + 3] << 8);

					Var_AddrRemapping(&p_add);

					sum = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
					V_max = config_buff[addr + 4] + (config_buff[addr + 5] << 8);
					V_min = config_buff[addr + 6] + (config_buff[addr + 7] << 8);
					step = config_buff[addr + 8] + (config_buff[addr + 9] << 8);


					if (sum + step > V_max)
					{
						var[p_add * 2] = V_max >> 8 & 0xFF;
						var[p_add * 2 + 1] = V_max & 0xFF;
					}
					else
					{
						sum += step;

						var[p_add * 2] = sum >> 8 & 0xFF;
						var[p_add * 2 + 1] = sum & 0xFF;
					}

					if (config_buff[addr] == 0x83)
					{
						rebuf[3] = 0x41;
						rebuf[4] = config_buff[addr + 3];
						rebuf[5] = config_buff[addr + 2];
						rebuf[6] = var[p_add * 2];
						rebuf[7] = var[p_add * 2 + 1];
						LT_SendData_CRC_Frame(rebuf, 5);
					}

					if (p_add >= VAR_ADDR_S)
						reg_operation(p_add);

					page_date_update(pic_id);

				}
				else
				{
					if (var[VAR_ENCODE * 2 + 1] == 1 && Gesture_flag == 1)
					{
						if (gGesture_Info.Code == 0xC000)
						{
							rebuf[3] = 0x41;
							rebuf[4] = 0xFF;
							rebuf[5] = 0xFF;
							rebuf[6] = (gGesture_Info.value[3] >> 8) & 0xFF;
							rebuf[7] = gGesture_Info.value[3] & 0xFF;
							LT_SendData_CRC_Frame(rebuf, 5);
						}
						Display_page(gGesture_Info.page[3]);
					}
					else
					{
						gEncoder_num++;
						if (gEncoder_num > gEncoder_count)
							gEncoder_num = 0;
						var[Check_box_add * 2] = (gEncoder_num >> 8) & 0xFF;
						var[Check_box_add * 2 + 1] = gEncoder_num & 0xFF;
						//LTPrintf("22222 = %d \r\n");
			if(pic_id==1){
			Menu_1_flag++;
			 if(Menu_1_flag>5)
				Menu_1_flag=5;
				var[0x0100*2+1] = Menu_1_flag;
			}
			if(pic_id==2){
			Menu_2_flag++;
			 if(Menu_2_flag>9)
				 Menu_2_flag=9;
				var[0x0200*2+1] = Menu_2_flag;
				if (Highlighted_state >= 1 && Highlighted_state <= 9 && Menu_2_flag >= 1 && Menu_2_flag <= 9) {
				    int index = Highlighted_state + 2 - Menu_2_flag;
				    for (int i = 0; i < 5; i++) {
				        var[varAddr[i]] = 0;
				    }
				    if (index >= 0 && index < 5) {
				        var[varAddr[index]] = 1;
				    }
				}
			}
			if(pic_id==4){
				Display_page(5);
				VOLgoto_flag=1;
				fanhui_flag=1;
				fanhui_timesum=0;
				var[0x0405*2+1] = 1;
			}
			if(pic_id==5){
				VOL_flag=VOL_flag+1;
			 if(VOL_flag>100)
				 VOL_flag=100;
				var[0x0406*2+1] = VOL_flag;
				send_data_ALL(0x01,VOL_flag,0x01,0x00,0xBB);
				 fanhui_flag=1;
				 fanhui_timesum=0;
			}
			if(pic_id==6){
				Display_page(5);
				VOLgoto_flag=2;
				fanhui_flag=1;
				fanhui_timesum=0;
				var[0x0405*2+1] = 1;
			}
			if(pic_id==7){
				Set_flag++;
			 if(Set_flag>2)
				 Set_flag=2;
				var[0x07BB*2+1] = Set_flag;
			}
			if(pic_id==8){
				Mega_bass_flag=Mega_bass_flag+1;
			 if(Mega_bass_flag>100)
				 Mega_bass_flag=100;
				var[0x080A*2+1] = Mega_bass_flag;
				send_data_ALL(0x07,Mega_bass_flag,0x01,0x00,0xBB);
				 BassFH_flag=1;
				 BassFH_time=0;
			}
			if(pic_id==9){
				Brightness_flag=Brightness_flag+1;
			 if(Brightness_flag>60)
				 Brightness_flag=60;
			 var[0x090B*2+1] = Brightness_flag;
			 LT_BacklightSet(Brightness_flag);
			 BassFH_flag=1;
			 BassFH_time=0;
			}
			if(pic_id==10||pic_id==11||pic_id==12){
				Page_sum++;
			 if(Page_sum>12)
				 Page_sum=12;
			    Display_page(Page_sum);
			}
			if (pic_id > 12 && pic_id < 18) {
			    Page_sum1++;
			    if (Page_sum1 > File_countB) {
			        Page_sum1 = File_countB;
			    }
			    send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
			    if (File_countB >= 5) {
			        int target_page;
			        if (Page_sum1 == 1) {
			            target_page = 13;
			        } else if (Page_sum1 == 2) {
			            target_page = 14;
			        } else if (Page_sum1 >= 3 && Page_sum1 <= File_countB - 2) {
			            target_page = 15;
			        } else if (Page_sum1 == File_countB - 1) {
			            target_page = 16;
			        } else if (Page_sum1 == File_countB) {
			            target_page = 17;
			        }
			        Display_page(target_page);
			    }
			}
		  page_date_update(pic_id);
					}
				}
			}
			else
			{
				if (Enc1 == 0)
				{
					Enc1 = 1;
					knob_dir = 1;
					Encoder_Freetime = 0;
				}
				else
				{
					if (knob_dir == 1)
					{
						Enc1 = 0;
						knob_dir = 0;

						if (knob_flag == 1) // Move right at knob control variable to increase
						{
							addr = gEncoder_addr[gEncoder_num];
							p_add = config_buff[addr + 2] + (config_buff[addr + 3] << 8);

							Var_AddrRemapping(&p_add);

							sum = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
							V_max = config_buff[addr + 4] + (config_buff[addr + 5] << 8);
							V_min = config_buff[addr + 6] + (config_buff[addr + 7] << 8);
							step = config_buff[addr + 8] + (config_buff[addr + 9] << 8);

							if (sum + step > V_max)
							{
								var[p_add * 2] = V_max >> 8 & 0xFF;
								var[p_add * 2 + 1] = V_max & 0xFF;
							}
							else
							{
								sum += step;

								var[p_add * 2] = sum >> 8 & 0xFF;
								var[p_add * 2 + 1] = sum & 0xFF;
							}

							if (config_buff[addr] == 0x83)
							{
								rebuf[3] = 0x41;
								rebuf[4] = config_buff[addr + 3];
								rebuf[5] = config_buff[addr + 2];
								rebuf[6] = var[p_add * 2];
								rebuf[7] = var[p_add * 2 + 1];
								LT_SendData_CRC_Frame(rebuf, 5);
							}

							if (p_add >= VAR_ADDR_S)
								reg_operation(p_add);

							page_date_update(pic_id);

						}
						else
						{
							if (var[VAR_ENCODE * 2 + 1] == 1 && Gesture_flag == 1)
							{
								if (gGesture_Info.Code == 0xC000)
								{
									rebuf[3] = 0x41;
									rebuf[4] = 0xFF;
									rebuf[5] = 0xFF;
									rebuf[6] = (gGesture_Info.value[3] >> 8) & 0xFF;
									rebuf[7] = gGesture_Info.value[3] & 0xFF;
									LT_SendData_CRC_Frame(rebuf, 5);
								}
								Display_page(gGesture_Info.page[3]);
							}
							else
							{
								gEncoder_num++;
								if (gEncoder_num > gEncoder_count)
									gEncoder_num = 0;
								var[Check_box_add * 2] = (gEncoder_num >> 8) & 0xFF;
								var[Check_box_add * 2 + 1] = gEncoder_num & 0xFF;
								page_date_update(pic_id);
							}
						}
					}
					else
					{
						knob_dir = 1;
						Enc1 = 1;
					}
				}
			}
		}
	}

	if ((EncOld == 0x00 && EncNow == 0x01) || (EncOld == 0x03 && EncNow == 0x02))
		EncX = EncNow; // 00-01|11-10

	// turn left,00-01-11|11-10-00
	if ((EncOld == 0x00 && EncX == 0x01 && EncNow == 0x03) || (EncOld == 0x03 && EncX == 0x02 && EncNow == 0x00))
	{
		Encoder_Freetime = 0;
		EncOld = EncNow, EncX = 0;
		Backlight_count = 0;
		//LTPrintf("120\r\n");
		if (var[VAR_BLAUTO * 2 + 1] ==1 && Backlight_flag == 0)
		{
			Backlight_flag = 1;
			LT_BacklightSet(gDutyBuf[var[VAR_BL * 2 + 1]]);
		}
		else if (SW_flag == 0 && SW_flag4 == 0)
		{
			if(EncType == 1)
			{
				if (knob_flag == 1) // At the knob control variable
				{
					addr = gEncoder_addr[gEncoder_num];
					p_add = config_buff[addr + 2] + (config_buff[addr + 3] << 8);

					Var_AddrRemapping(&p_add);

					sum = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
					V_max = config_buff[addr + 4] + (config_buff[addr + 5] << 8);
					V_min = config_buff[addr + 6] + (config_buff[addr + 7] << 8);
					step = config_buff[addr + 8] + (config_buff[addr + 9] << 8);


					if (sum - step < V_min)
					{
						var[p_add * 2] = V_min >> 8 & 0xFF;
						var[p_add * 2 + 1] = V_min & 0xFF;
					}
					else
					{
						sum -= step;
						var[p_add * 2] = sum >> 8 & 0xFF;
						var[p_add * 2 + 1] = sum & 0xFF;
					}
					if (config_buff[addr] == 0x83)
					{
						rebuf[3] = 0x41;
						rebuf[4] = config_buff[addr + 3];
						rebuf[5] = config_buff[addr + 2];
						rebuf[6] = var[p_add * 2];
						rebuf[7] = var[p_add * 2 + 1];
						LT_SendData_CRC_Frame(rebuf, 5);
					}
					if (p_add >= VAR_ADDR_S)
						reg_operation(p_add);

					page_date_update(pic_id);
				}
				else
				{
					if (var[VAR_ENCODE * 2 + 1] == 1 && Gesture_flag == 1)
					{
						if (gGesture_Info.Code == 0xC000)
						{
							rebuf[3] = 0x41;
							rebuf[4] = 0xFF;
							rebuf[5] = 0xFF;
							rebuf[6] = (gGesture_Info.value[2] >> 8) & 0xFF;
							rebuf[7] = gGesture_Info.value[2] & 0xFF;
							LT_SendData_CRC_Frame(rebuf, 5);
						}
						Display_page(gGesture_Info.page[2]);
					}
					else
					{
						if (gEncoder_num == 0)
							gEncoder_num = gEncoder_count;
						else
							gEncoder_num--;
						var[Check_box_add * 2] = (gEncoder_num >> 8) & 0xFF;
						var[Check_box_add * 2 + 1] = gEncoder_num & 0xFF;
						//LTPrintf("111111111111 = %d \r\n");
			if(pic_id==1){
			 Menu_1_flag--;
			 if(Menu_1_flag<1)
				Menu_1_flag=1;
				var[0x0100*2+1] = Menu_1_flag;
			}
			if(pic_id==2){
				Menu_2_flag--;
			 if(Menu_2_flag<1)
				 Menu_2_flag=1;
				var[0x0200*2+1] = Menu_2_flag;
				if (Highlighted_state >= 1 && Highlighted_state <= 9 && Menu_2_flag >= 1 && Menu_2_flag <= 9) {
				    int index = Highlighted_state + 2 - Menu_2_flag;
				    for (int i = 0; i < 5; i++) {
				        var[varAddr[i]] = 0;
				    }
				    if (index >= 0 && index < 5) {
				        var[varAddr[index]] = 1;
				    }
				}
			}
			if(pic_id==4){
				Display_page(5);
				VOLgoto_flag=1;
				fanhui_flag=1;
				fanhui_timesum=0;
				var[0x0405*2+1] = 1;
			}
			if(pic_id==5){
				VOL_flag=VOL_flag-1;
			 if(VOL_flag<0)
				 VOL_flag=0;
				var[0x0406*2+1] = VOL_flag;
				send_data_ALL(0x01,VOL_flag,0x01,0x00,0xBB);
				 fanhui_flag=1;
				 fanhui_timesum=0;
			}
			if(pic_id==6){
				Display_page(5);
				VOLgoto_flag=2;
				fanhui_flag=1;
				fanhui_timesum=0;
				var[0x0405*2+1] = 1;
			}
			if(pic_id==7){
				Set_flag--;
			 if(Set_flag<1)
				 Set_flag=1;
				var[0x07BB*2+1] = Set_flag;
			}
			if(pic_id==8){
				Mega_bass_flag=Mega_bass_flag-1;
			 if(Mega_bass_flag<0)
				 Mega_bass_flag=0;
				var[0x080A*2+1] = Mega_bass_flag;
				send_data_ALL(0x07,Mega_bass_flag,0x01,0x00,0xBB);
				 BassFH_flag=1;
				 BassFH_time=0;
			}
			if(pic_id==9){
				Brightness_flag=Brightness_flag-1;
			 if(Brightness_flag<1)
				 Brightness_flag=1;
			 var[0x090B*2+1] = Brightness_flag;
			 LT_BacklightSet(Brightness_flag);
			 BassFH_flag=1;
			 BassFH_time=0;
			}
			if(pic_id==10||pic_id==11||pic_id==12){
				Page_sum--;
			 if(Page_sum<10)
				 Page_sum=10;
			    Display_page(Page_sum);
			}
			if (pic_id > 12 && pic_id < 18) {
			    Page_sum1--;
			    if (Page_sum1 < 1) {
			        Page_sum1 = 1;
			    }
			    send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
			    if (File_countB >= 5) {
			        int target_page;
			        if (Page_sum1 == 1) {
			            target_page = 13;
			        } else if (Page_sum1 == 2) {
			            target_page = 14;
			        } else if (Page_sum1 >= 3 && Page_sum1 <= File_countB - 2) {
			            target_page = 15;
			        } else if (Page_sum1 == File_countB - 1) {
			            target_page = 16;
			        } else if (Page_sum1 == File_countB) {
			            target_page = 17;
			        }
			        Display_page(target_page);
			    }
			}
		 page_date_update(pic_id);
					}
				}
			}
			else
			{
				if (Enc1 == 0) // First rotation
				{
					knob_dir = 2;
					Enc1 = 1;
					Encoder_Freetime = 0;
				}
				else // Second rotation within 1S between the first rotation
				{
					if (knob_dir == 2)
					{
						Enc1 = 0;
						knob_dir = 0;
						if (knob_flag == 1) // At the knob control variable
						{
							addr = gEncoder_addr[gEncoder_num];
							p_add = config_buff[addr + 2] + (config_buff[addr + 3] << 8);

							Var_AddrRemapping(&p_add);

							sum = (var[p_add * 2] << 8) + var[p_add * 2 + 1];
							V_max = config_buff[addr + 4] + (config_buff[addr + 5] << 8);
							V_min = config_buff[addr + 6] + (config_buff[addr + 7] << 8);
							step = config_buff[addr + 8] + (config_buff[addr + 9] << 8);


							if (sum - step < V_min)
							{
								var[p_add * 2] = V_min >> 8 & 0xFF;
								var[p_add * 2 + 1] = V_min & 0xFF;
							}
							else
							{
								sum -= step;
								var[p_add * 2] = sum >> 8 & 0xFF;
								var[p_add * 2 + 1] = sum & 0xFF;
							}
							if (config_buff[addr] == 0x83)
							{
								rebuf[3] = 0x41;
								rebuf[4] = config_buff[addr + 3];
								rebuf[5] = config_buff[addr + 2];
								rebuf[6] = var[p_add * 2];
								rebuf[7] = var[p_add * 2 + 1];
								LT_SendData_CRC_Frame(rebuf, 5);
							}
							if (p_add >= VAR_ADDR_S)
								reg_operation(p_add);

							page_date_update(pic_id);
						}
						else
						{
							if (var[VAR_ENCODE * 2 + 1] == 1 && Gesture_flag == 1)
							{
								if (gGesture_Info.Code == 0xC000)
								{
									rebuf[3] = 0x41;
									rebuf[4] = 0xFF;
									rebuf[5] = 0xFF;
									rebuf[6] = (gGesture_Info.value[2] >> 8) & 0xFF;
									rebuf[7] = gGesture_Info.value[2] & 0xFF;
									LT_SendData_CRC_Frame(rebuf, 5);
								}
								Display_page(gGesture_Info.page[2]);
							}
							else
							{
								if (gEncoder_num == 0)
									gEncoder_num = gEncoder_count;
								else
									gEncoder_num--;
								var[Check_box_add * 2] = (gEncoder_num >> 8) & 0xFF;
								var[Check_box_add * 2 + 1] = gEncoder_num & 0xFF;
								page_date_update(pic_id);
							}
						}
					}
					else
					{
						knob_dir = 2;
						Enc1 = 1;
					}
				}
			}
		}
	}

	if(SW_flag)
	{
		Backlight_count = 0;
		if (var[VAR_BLAUTO * 2 + 1] ==1 && Backlight_flag == 0)
		{
			SW_flag = 0;
			SW_flag4 = 0;
			Backlight_flag = 1;
			LT_BacklightSet(gDutyBuf[var[VAR_BL * 2 + 1]]);
		}
		else if (var[VAR_ENCODE * 2 + 1] == 1 && Gesture_flag == 1)
		{
			var[VAR_ENCODE * 2 + 1] = 0;
			SW_flag = 0;
			SW_flag4 = 0;
		}
		else
		{
			if (SW_flag == 1)
			{
				SW_flag = 0;
				Enc1 = 0;
				knob_dir = 0;
				Display_encoder(&config_buff[gEncoder_addr[gEncoder_num]], 0);
			}
			else if (SW_flag4 == 3 && SW_flag == 4)
			{
				//LTPrintf("44444BwarmBlast_flag = %d \r\n",warmBlast_flag);
				if(pic_id==2||pic_id==4||pic_id==6||pic_id==7||pic_id==10||pic_id==11||pic_id==12||pic_id==18)
				{
				 Display_page(1);
				}
				else if(pic_id==5)
			    {
				  if(Usb_flag==1)
				  {
				   Display_page(4);
				  }
				  else
				   Display_page(6);
			    }
				if(pic_id==8||pic_id==9)
				{
				 Display_page(7);
				}
				if(pic_id==3)
				{
				 Display_page(2);
				}
				else if(pic_id > 12 && pic_id < 18)
				{
					File_level--;
				    if (File_level <0)
				    	File_level = 0;
					    Page_sum1=1;
						if(File_level>0){Display_page(13);}
						else if(File_level==0){Display_page(1);}
						var[0x0EA0*2+1] = File_level;
						send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
					 for (int i = 0; i < 5; i++) {
					     int addr = 0x1011 + i * 0x20;
					     var[addr * 2] = 0;
					     var[addr * 2 + 1] = 0;
					 }
				}
				SW_flag = 0;
				Enc1 = 0;
				knob_dir = 0;
				if ((config_buff[gEncoder_addr[gEncoder_num]] & 0x2F) == 0x24)
					type = 0x84;
				else
					type = 4;
				Display_encoder(&config_buff[gEncoder_addr[gEncoder_num] + 72], type);
			}
			else if (SW_flag4 == 2 && SW_flag == 3)
			{
				//LTPrintf("55555BwarmBlast_flag = %d \r\n",warmBlast_flag);
				if(pic_id==4||pic_id==6)
				{
					 Danji_flag++;
					 if(Danji_flag==1)
					 {
						var[0x0405*2+1] = 2;
						send_data_ALL(0x01,VOL_flag,0x00,0x00,0xBB);
					 }
					 else if(Danji_flag==2)
					 {
					    var[0x0405*2+1] = 1;
					    send_data_ALL(0x01,VOL_flag,0x01,0x00,0xBB);
			            Danji_flag=0;
					 }
				}
				SW_flag = 0;
				Enc1 = 0;
				knob_dir = 0;
				if ((config_buff[gEncoder_addr[gEncoder_num]] & 0x4F) == 0x44)
					type = 0x84;
				else
					type = 4;
				Display_encoder(&config_buff[gEncoder_addr[gEncoder_num] + 37], type);
			}
			else if (SW_flag4 == 1 && SW_flag == 2)
			{
				//LTPrintf("3333BwarmBlast_flag = %d \r\n",warmBlast_flag);
				if(pic_id==1){
					if(Menu_1_flag==1)
					{
						Display_page(13);
						File_level = 1;
						Page_sum1=1;
						var[0x0EA0*2+1] = 1;
						send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
						//uint8_t data_part[] = {0xAA,0xA5,0x0C,0x10,0x10,0x11 ,0x66,0x3E,0x79,0x3A,0x00,0x31,0x00,0x00 ,0x47};
						//LT_SendData_CRC_Frame(data_part, 11);
					}
					else if(Menu_1_flag==2){
						if(Usb_flag==1){
					      Display_page(4);
						}
						else
						  Display_page(6);
					}
					else if(Menu_1_flag==3){
					   Display_page(2);
					}
					else if(Menu_1_flag==4){
					   Display_page(7);
					}
					else if(Menu_1_flag==5){
					   Display_page(10);
					}
				}
				else if(pic_id==2)//SRC按下数据
				{
					send_data_ALL(0x02,Menu_2_flag,0x00,0x00,0xBB);
					var[0x0400*2+1]=Menu_2_flag;
					//Display_page(3);
					var[0x0471*2+1] = 10;
					var[0x0472*2+1] = 1;
					  if(Menu_2_flag==1||Menu_2_flag==2||Menu_2_flag==3){
						  Usb_flag=1;
					  }
					  else
						  Usb_flag=0;
					  Highlighted_state=Menu_2_flag;
						for (int i = 0; i < 5; i++) {
							var[varAddr[i]] = 0;
						}
						var[varAddr[2]]=1;
				}
				else if(pic_id==4){
					 Danji1_flag++;
					 if(Danji1_flag==1){
						 Play_pause=1;
					    var[0x020C*2+1] = Play_pause;
					    var[0x0403*2+1] = Play_pause;
						var[0x0403*2+1] = var[0x020C*2+1];
						send_data_ALL(0x03,Play_pause,0x00,0x00,0xBB);
					 }
					 else if(Danji1_flag==2){
						 Play_pause=0;
					    var[0x020C*2+1] = Play_pause;
						var[0x0403*2+1] = var[0x020C*2+1];
						send_data_ALL(0x03,Play_pause,0x00,0x00,0xBB);
					    Danji1_flag=0;
					 }
				}
				else if(pic_id==7){
					BassFH_flag=1;
					BassFH_time=0;
					if(Set_flag==1){
					  Display_page(8);
					}
					else
					  Display_page(9);
				}
				else if(pic_id > 12 && pic_id < 18){
					File_level++;
				    if (File_level > set_Filelevel)
				    	File_level = set_Filelevel;
				     Display_page(13);
				     Page_sum1=1;
					 var[0x0EA0*2+1] = File_level;
				     send_data_ALL(0x08,(Page_sum1 >> 8) & 0xFF,Page_sum1 & 0xFF,File_level,0xBB);
					 for (int i = 0; i < 5; i++) {
					     int addr = 0x1011 + i * 0x20;
					     var[addr * 2] = 0;
					     var[addr * 2 + 1] = 0;
					 }
				}
				SW_flag4 = 0;
				SW_flag = 0;
				Enc1 = 0;
				knob_dir = 0;
				if ((config_buff[gEncoder_addr[gEncoder_num]] & 0x8F) == 0x84)
					type = 0x84;
				else
					type = 4;
				Display_encoder(&config_buff[gEncoder_addr[gEncoder_num] + 2], type);
			}
		}
	}
	if (SW_flag4 == 2 || SW_flag4 == 3)
	{
		Backlight_count = 0;
		if (Read_Encoder_key == 1)
		{
			SW_flag4 = 0;
			Encoder_read_Flag = 0;
			Encoder_time = 0;
			key_time = 0;
		}
	}
}


